// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: K Logo Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

private alias object.frame : object.knuxFrame

private alias object.value0 : object.timer
private alias object.value1 : object.knuxPos.x
private alias object.value2 : object.knuxPos.y
private alias object.value3 : object.levelSelectCheatPos	// not used in this script, used in "STLogo_CheckButtonCheatCodes"
private alias object.value4 : object.regionSwitchCheatPos		// not used in this script, used in "STLogo_CheckButtonCheatCodes"
private alias object.value5 : object.handFrame
private alias object.value6 : object.handPos.x
private alias object.value7 : object.handPos.y
private alias object.value8 : object.knuxInPos.y

// States
private alias 0  : KLOGO_SETUP
private alias 1  : KLOGO_FADEIN
private alias 2  : KLOGO_SHOWEMBLEM
private alias 3  : KLOGO_ENTERKNUX
private alias 4  : KLOGO_KNUXANIMATE
private alias 5  : KLOGO_ENTERHAND
private alias 6  : KLOGO_HANDANIMATE
private alias 7  : KLOGO_FINISHHANDANIM
private alias 8  : KLOGO_MOVEKNUXINTOPLACE
private alias 9  : KLOGO_MOVEHAND2
private alias 10 : KLOGO_HANDANIMATE2
private alias 11 : KLOGO_FINISHHANDANIM2
private alias 12 : KLOGO_ENTERKNUXIN
private alias 13 : KLOGO_FINISHEDANIMS

// ShootingStar Aliases
private alias object.value0 : shootingStar.delay

// Game Modes
private alias 0 : MODE_NOSAVE


// ========================
// Events
// ========================

event ObjectUpdate
	if options.gameMode == MODE_NOSAVE
		if object.state > KLOGO_SHOWEMBLEM
			CallFunction(STLogo_CheckButtonCheatCodes)
		end if
	end if

	switch object.state
	case KLOGO_SETUP
		if object.timer == 30
			object[+1].type = TypeName[Twinkle Stars]
			object[+1].priority = PRIORITY_ACTIVE
		end if

		if object.timer < 60
			object.timer++
		else
			object.timer 			= 0
			object.alpha 			= 256
			stage.player2Enabled 	= false
			object.state++
		end if
		break

	case KLOGO_FADEIN
		if object.alpha > 0
			object.alpha -= 8
		else
			object.state++
		end if
		break

	case KLOGO_SHOWEMBLEM
		if object.timer < 32
			object.timer++
		else
			object.timer = 0
			object.knuxPos.x = -0x2C0000
			object.knuxPos.x += object.xpos
			object.knuxPos.y = -0x160000
			object.knuxPos.y += object.ypos
			object.knuxFrame = 2
			PlayMusic(0)
			object.state++
		end if
		break

	case KLOGO_ENTERKNUX
		object.knuxPos.y -= 0x80000
		if object.knuxPos.y <= 0x1A0000
			object.state++
		end if

		if object.knuxPos.y == 0x3A0000
			object.knuxFrame++
			object.handFrame = 8
			object.handPos.x = -0x2C0000
			object.handPos.x += object.xpos
			object.handPos.y = -0x2E0000
			object.handPos.y += object.ypos
		end if
		break

	case KLOGO_KNUXANIMATE
		object.timer++
		if object.timer == 8
			object.knuxFrame++
			if object.knuxFrame >= 7
				object.knuxFrame = 7
				object.state++
			end if
			object.timer = 0
		end if
		break

	case KLOGO_ENTERHAND
		object.handPos.y -= 0x10000
		object.timer++
		if object.timer == 4
			object.timer = 0
			object.state++
		end if
		break

	case KLOGO_HANDANIMATE
		object.timer++
		if object.timer == 4
			object.timer = 0
			object.handFrame++
			if object.handFrame == 13
				object.handFrame = 8
				object.state++
			end if
		end if
		break

	case KLOGO_FINISHHANDANIM
		object.handPos.y += 0x10000

		object.timer++
		if object.timer == 4
			object.timer = 0
			object.state++
		end if
		break

	case KLOGO_MOVEKNUXINTOPLACE
		object.yvel += 0x4000
		object.ypos += object.yvel

		object.knuxPos.y += object.yvel
		object.handPos.y += object.yvel
		if object.yvel > 0
			if object.ypos >= 0x900000
				object.yvel >>= 1
				temp0 = object.yvel
				temp0 >>= 1
				object.yvel += temp0
				object.timer++
				if object.timer != 3
					FlipSign(object.yvel)
				else
					object.timer 			= 0
					object.yvel 			= 0
					stage.activeLayer[0] 	= 2 // Show Title BG
					object.alpha 			= 0x100
					object.state++
				end if
			end if
		end if
		break

	case KLOGO_MOVEHAND2
		object.handPos.y -= 0x10000

		object.timer++
		if object.timer == 4
			object.timer = 0
			object.state++
		end if

		if object.alpha > 0
			object.alpha -= 12
		end if
		break

	case KLOGO_HANDANIMATE2
		object.timer++
		if object.timer == 4
			object.timer = 0
			object.handFrame++
			if object.handFrame == 13
				object.handFrame = 8
				object.state++
			end if
		end if

		if object.alpha > 0
			object.alpha -= 12
		end if
		break

	case KLOGO_FINISHHANDANIM2
		if object.timer < 4
			object.handPos.y += 0x10000

			object.timer++
			if object.alpha > 0
				object.alpha -= 12
			end if
		else
			if object.alpha > 0
				object.alpha -= 12
			else
				object.timer = 0
				object.state++

				object[+1].type = TypeName[Start Message]
				object[+1].priority = PRIORITY_ACTIVE
				object[+1].xpos = screen.xcenter
				object[+1].ypos = 200

				CreateTempObject(TypeName[Shooting Star], 0, object.xpos, object.ypos)
				object[tempObjectPos].drawOrder = 2
				object[tempObjectPos].xpos += 0x1600000
				object[tempObjectPos].ypos -= 0x1000000
				shootingStar[tempObjectPos].delay = 150
			end if
		end if
		break

	case KLOGO_ENTERKNUXIN
		object.yvel += 0x4000
		object.knuxInPos.y += object.yvel

		if object.yvel > 0
			if object.knuxInPos.y >= 0x5A0000
				object.yvel >>= 2
				temp0 = object.yvel
				temp0 >>= 1
				object.yvel += temp0
				object.timer++
				if object.timer != 2
					FlipSign(object.yvel)
				else
					object.timer = 0
					object.yvel = 0
					object.state++
				end if
			end if
		end if

		object.animationTimer++
		if object.animationTimer > 7
			tileLayer[2].deformationOffset++
			object.animationTimer = 0
		end if
		break

	case KLOGO_FINISHEDANIMS
		object.animationTimer++
		if object.animationTimer > 7
			tileLayer[2].deformationOffset++
			object.animationTimer = 0
		end if
		break
		
	end switch

	temp0 = screen.xcenter
	FlipSign(temp0)
	temp0 += 256
	screen.xoffset = temp0
end event


event ObjectDraw
	switch object.state
	case KLOGO_SETUP
		ClearScreen(1)
		break

	case KLOGO_FADEIN
		ClearScreen(1)

		// Draw Emblem (Top)
		DrawSprite(0)
		
		// Draw Emblem (Bottom)
		DrawSprite(1)

		DrawRect(0, 0, screen.xsize, screen.ysize, 0x00, 0x00, 0x00, object.alpha)
		break

	case KLOGO_SHOWEMBLEM
		ClearScreen(1)

		// Draw Emblem (Top)
		DrawSprite(0)
		
		// Draw Emblem (Bottom)
		DrawSprite(1)
		break

	case KLOGO_ENTERKNUX
		ClearScreen(1)

		// Draw Emblem (Top)
		DrawSprite(0)

		// Draw Knux
		DrawSpriteXY(object.knuxFrame, object.knuxPos.x, object.knuxPos.y)
		
		// Draw Emblem (Bottom)
		DrawSprite(1)
		break

	case KLOGO_KNUXANIMATE
		ClearScreen(1)

		// Draw Emblem (Top)
		DrawSprite(0)

		// Draw Knux
		DrawSpriteXY(object.knuxFrame, object.knuxPos.x, object.knuxPos.y)
		if object.knuxFrame >= 7
			DrawSpriteXY(object.handFrame, object.handPos.x, object.handPos.y)
		end if
		
		// Draw Emblem (Bottom)
		DrawSprite(1)
		break

	case KLOGO_ENTERHAND
	case KLOGO_HANDANIMATE
	case KLOGO_FINISHHANDANIM
	case KLOGO_MOVEKNUXINTOPLACE
		ClearScreen(1)

		// Draw Emblem (Top)
		DrawSprite(0)

		// Draw Knux
		DrawSpriteXY(object.knuxFrame, object.knuxPos.x, object.knuxPos.y)
		DrawSpriteXY(object.handFrame, object.handPos.x, object.handPos.y)
		
		// Draw Emblem (Bottom)
		DrawSprite(1)
		break

	case KLOGO_MOVEHAND2
	case KLOGO_HANDANIMATE2
	case KLOGO_FINISHHANDANIM2
		DrawRect(0, 0, screen.xsize, screen.ysize, 0xE0, 0xE0, 0xE0, object.alpha)

		// Draw Emblem (Top)
		DrawSprite(0)

		// Draw Knux
		DrawSpriteXY(object.knuxFrame, object.knuxPos.x, object.knuxPos.y)
		DrawSpriteXY(object.handFrame, object.handPos.x, object.handPos.y)
		
		// Draw Emblem (Bottom)
		DrawSprite(1)

		temp0 = screen.xsize
		temp0 -= 96
		DrawSpriteScreenXY(15, temp0, 216)
		break

	case KLOGO_ENTERKNUXIN
	case KLOGO_FINISHEDANIMS
		// Draw Emblem (Top)
		DrawSprite(0)

		// Draw Knux
		DrawSpriteXY(object.knuxFrame, object.knuxPos.x, object.knuxPos.y)
		DrawSpriteXY(object.handFrame, object.handPos.x, object.handPos.y)

		// Draw Emblem (Bottom)
		DrawSprite(1)

		// Knux In
		DrawSpriteXY(14, object.xpos, object.knuxInPos.y)

		// Copyright
		temp0 = screen.xsize
		temp0 -= 96
		DrawSpriteScreenXY(15, temp0, 216)
		break
		
	end switch
end event


event ObjectStartup
	LoadSpriteSheet("Title/Title.gif")
	SpriteFrame(-128, -78, 256, 82, 255, 67)	// Emblem (Top) 		- #0
	SpriteFrame(-128, -8, 256, 87, 255, 150)	// Emblem (Bottom) 		- #1
	SpriteFrame(3, 15, 101, 47, 1, 438)			// Knux - Frame 1 		- #2
	SpriteFrame(3, 15, 101, 73, 1, 438)			// Knux - Frame 2 		- #3
	SpriteFrame(0, 9, 95, 79, 103, 432)			// Knux - Frame 3 		- #4
	SpriteFrame(1, 6, 105, 82, 199, 429)		// Knux - Frame 4 		- #5
	SpriteFrame(1, 4, 102, 84, 305, 427)		// Knux - Frame 5 		- #6
	SpriteFrame(5, 2, 91, 86, 408, 425)			// Knux - Frame 6 		- #7
	SpriteFrame(8, 5, 42, 43, 180, 138)			// Knux Hand - Frame 1 	- #8
	SpriteFrame(8, 3, 40, 53, 465, 1)			// Knux Hand - Frame 2 	- #9
	SpriteFrame(8, 8, 40, 39, 149, 182)			// Knux Hand - Frame 3 	- #10
	SpriteFrame(8, 8, 40, 39, 149, 182)			// Knux Hand - Frame 4 	- #11
	SpriteFrame(8, 3, 40, 53, 465, 1)			// Knux Hand - Frame 5 	- #12
	SpriteFrame(8, 5, 42, 43, 180, 138)			// Knux Hand - Frame 6 	- #13
	SpriteFrame(-124, -66, 248, 65, 263, 359)	// Knux IN 				- #14
	SpriteFrame(0, 0, 88, 16, 1, 421)			// Copyright 			- #15
end event


// ========================
// Editor Events
// ========================

event RSDKDraw
	DrawSprite(0)
	DrawSprite(1)
end event


event RSDKLoad
	LoadSpriteSheet("Title/Title.gif")
	SpriteFrame(-128, -78, 256, 82, 255, 67)	// Emblem (Top)		- #0
	SpriteFrame(-128, -8, 256, 87, 255, 150)	// Emblem (Bottom) 	- #1
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
